import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MTCodeExtractor {
    public static void main(String[] args) {
        String description = "Machine Types (MT Code: 8023, MT Code: 2abc, MT Code: 812, MT Code: 2034, MT Code: 8023)";

        List<String> mtCodes = extractValidMTCodes(description);
        System.out.println(mtCodes); // 输出: [8023, 2034]
    }

    public static List<String> extractValidMTCodes(String description) {
        // 正则匹配：以 (MT Code: 开头，后跟2/8开头的4位数字或字母（不区分大小写）
        Pattern pattern = Pattern.compile("\\(MT Code:\\s*([28][0-9A-Fa-f]{3})\\)", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(description);

        Set<String> uniqueCodes = new LinkedHashSet<>(); // 有序去重

        while (matcher.find()) {
            String code = matcher.group(1).toUpperCase(); // 统一转大写
            if (code.matches("[28][0-9A-F]{3}")) { // 二次校验（确保只有数字和大写字母）
                uniqueCodes.add(code);
            }
        }

        return new ArrayList<>(uniqueCodes);
    }
}